package com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.share;

import android.util.Log;

import androidx.annotation.NonNull;

import com.alibaba.sdk.android.oss.OSSClient;
import com.coffeeloversupportlibrary.coffeelover.ApiService;
import com.coffeeloversupportlibrary.coffeelover.callback.ModelCallBack;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.share.entity.OriginShareImageUrl;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.share.entity.UpLoadResponse;
import com.coffeeloversupportlibrary.coffeelover.domain.dailyshare.share.entity.UpLoadShare;
import com.coffeeloversupportlibrary.core.aliyun.upload.UpLoadFile;
import com.coffeeloversupportlibrary.core.base.BaseModel;
import com.coffeeloversupportlibrary.core.data.IDataRepository;
import com.google.gson.Gson;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @author puyv
 * @createTime 2021/11/24 9:30
 * @projectName CoffeeLoverSupportLibrary
 * @className ShareModel.java
 * @description TODO
 */
public class ShareModel extends BaseModel {
    private Gson gson;
    public ShareModel(IDataRepository dataRepository) {
        super(dataRepository);
    }


    public void uploadImage(@NonNull File file, @NonNull OSSClient client, ImageLoadCallback<String> callback){
        UpLoadFile.asyUpLoad(file, client, new UpLoadFile.OATCallback() {
            @Override
            public void success() {
                // TODO: 2021/11/27 成功的回调
                callback.loadSuccess("https://mercuryimg.oss-cn-beijing.aliyuncs.com/shareImg/");
            }

            @Override
            public void failure() {
                // TODO: 2021/11/27  失败的回调
            }
        });

    }

    public void uploadShare(String title,String content,String url,ModelCallBack<String,String> callBack){

        UpLoadShare share=new UpLoadShare();
        List<UpLoadShare.PicListDTO> picture=new ArrayList<>();

        UpLoadShare.PicListDTO picListDTO=new UpLoadShare.PicListDTO();
        picListDTO.setImgOrder(1);
        picListDTO.setShareImg(url);

        picture.add(picListDTO);

        share.setUserId("10bc96d2-6003-430a-95f2-710209e67e0a");
        share.setShareContent(content);
        share.setPicList(picture);
        gson=new Gson();

        String json=gson.toJson(share);
        RequestBody requestBody = RequestBody.create(
                MediaType.parse("application/json"),
                json);

        getRetrofitService(ApiService.class).upLoadShare(requestBody)
                .enqueue(new Callback<UpLoadResponse>() {
                    @Override
                    public void onResponse(Call<UpLoadResponse> call, Response<UpLoadResponse> response) {
                           if(response.body().getCode()==20000){
                               callBack.onSuccess("success");
                           }
                    }

                    @Override
                    public void onFailure(Call<UpLoadResponse> call, Throwable t) {
                        callBack.onSuccess("SHARE"+t.getMessage());
                        Log.e("SHARE","分享失败");
                    }
                });


    }

    public interface ImageLoadCallback<T>{
        public void loadSuccess(T t);
        public void loadFailure(T t);
    }
}
